<!--
 * @Description: Description
 * @Author: Yongchao Wang
 * @Date: 2020-09-10 08:37:58
 * @LastEditors: Yongchao Wang
 * @LastEditTime: 2020-09-11 07:08:48
-->
<template>
  <!-- optionAPI -->
  <div class="hello">
    <input type="number" v-model="num1" @keyup="add" />
    <span>+</span>
    <input type="number" v-model="num2" @keyup="add" />
    <span>=</span>
    {{result}}
    <button @click="click">emit</button>
  </div>

  <!-- Composition API reactive computed-->
  <div>
    <input type="number" v-model="state.num3" @keyup="add1" />
    <span>+</span>
    <input type="number" v-model="state.num4" @keyup="add1" />
    <span>=</span>
    {{state.result1}}
    <button @click="click1">emit1</button>
  </div>
</template>

<script>
import { computed, reactive } from "vue";

export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      num1: 0,
      num2: 0,
      result: 0,
    };
  },
  computed: {
    result() {
      return parseInt(this.num1) + parseInt(this.num2);
    },
  },
  // Composition API reactive computed
  setup(props, { emit }) {
    const state = reactive({
      num3: 0,
      num4: 0,
      result1: computed(() => parseInt(state.num3) + parseInt(state.num4)),
    });
    function add1() {
      state.result1 = parseInt(state.num3) + parseInt(state.num4);
    }
    const click1 = () => {
      emit("sendMsg", 123);
    };
    return {
      state,
      click1,
    };
  },
  methods: {
    click() {
      this.$emit("sendMsg", 123);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
